class Left {
  static of (val) {
    return new Left(val)
  }

  constructor(value) {
    this._value = value
  }

  map(fn) {
    return this
  }
}

class Right {
  static of (val) {
    return new Right(val)
  }

  constructor(value) {
    this._value = value
  }

  map(fn) {
    return Right.of(fn(this._value))
  }
}

// let r1 = Right.of(12).map(item => item + 2)
// let r2 = Left.of(12).map(item => item * 2)

// console.log(r1)
// console.log(r2)

function parseJSON(str) {
  try {
    return Right.of(JSON.parse(str))
  } catch (error) {
    return Left.of({ error: error.message})
  }
}
let r = parseJSON('{"name": "zs"}').map(val => val.name.toUpperCase())
console.log(r)

